package com.thiscc.sum.logic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.thiscc.sum.common.MongoDBManager;
import com.thiscc.sum.constance.TableName;
import com.thiscc.sum.utils.DBObjectUtils;
import com.thiscc.sum.utils.DateUtils;
import com.thiscc.sum.utils.RequestUtils;
import com.thiscc.sum.utils.StringUtils;

/**
 * 
 * @author helios
 * @since  1.0
 *
 */
public class WorkLogic {

    //数据库操作对象
    private MongoDBManager db = MongoDBManager.getInstance();

    /*-------------------------------------------------------------------------------------------*/

    public JSONObject getWorkList(HttpServletRequest request) {

        String s_uuid = RequestUtils.getString(request, "uuid");
        request.getSession().setAttribute("uuid", s_uuid);
        String s_ccno = RequestUtils.getString(request, "ccno");
        request.getSession().setAttribute("ccno", s_ccno);
        String s_realname = RequestUtils.getString(request, "realname");
        request.getSession().setAttribute("realname", s_realname);

        //查询置顶的数据
        DBObject DBO_condition = new BasicDBObject();
        DBO_condition.put("uuid", s_uuid);
        DBO_condition.put("show", "1");
        DBO_condition.put("insteadTop", "1");

        List<DBObject> topList = db.find(TableName.T_WORK, DBO_condition);

        //查询非置顶数据
        DBO_condition.put("insteadTop", "0");
        List<DBObject> notTopList = db.find(TableName.T_WORK, DBO_condition);

        JSONObject JSY_json = new JSONObject();

        JSONArray JSY_top = new JSONArray();
        JSONArray JSY_notTop = new JSONArray();

        for (int i = 0; i < topList.size(); i++) {

            DBObject topDB = topList.get(i);
            Date time = (Date) topDB.get("time");
            JSONObject topJson = JSONObject.fromObject(topDB);
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = format.format(time);
            topJson.put("time", dateString);
            topJson.put("timeLong", time.getTime());
            JSY_top.add(topJson);
        }

        for (int i = 0; i < notTopList.size(); i++) {

            DBObject notTopDB = notTopList.get(i);
            Date time = (Date) notTopDB.get("time");
            JSONObject notTopJson = JSONObject.fromObject(notTopDB);
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = format.format(time);
            notTopJson.put("time", dateString);
            notTopJson.put("timeLong", time.getTime());
            JSY_notTop.add(notTopJson);
        }

        JSY_json.put("top", JSY_top.toString());
        JSY_json.put("notTop", JSY_notTop.toString());

        return JSY_json;
    }

    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> getAllWorkList(HttpServletRequest request) {

        String s_uuid = RequestUtils.getString(request, "uuid");
        //查询置顶的数据
        DBObject DBO_condition = new BasicDBObject();
        DBO_condition.put("uuid", s_uuid);

        List<DBObject> topList = db.find(TableName.T_WORK, DBO_condition);

        return topList;
    }

    /*-------------------------------------------------------------------------------------------*/

    public JSONArray getShowWorkList(HttpServletRequest request) {

        String s_uuid = RequestUtils.getString(request, "uuid");
        request.getSession().setAttribute("uuid", s_uuid);
        String s_ccno = RequestUtils.getString(request, "ccno");
        request.getSession().setAttribute("ccno", s_ccno);
        String s_realname = RequestUtils.getString(request, "realname");
        request.getSession().setAttribute("realname", s_realname);

        //查询置顶的数据
        DBObject DBO_condition = new BasicDBObject();
        DBO_condition.put("uuid", s_uuid);
        DBO_condition.put("show", "1");

        List<DBObject> list = db.find(TableName.T_WORK, DBO_condition);

        JSONArray JSY_array = new JSONArray();

        for (int i = 0; i < list.size(); i++) {

            DBObject dbo = list.get(i);
            Date time = DBObjectUtils.getDate(dbo, "time");
            JSONObject json = JSONObject.fromObject(dbo);
            json.put("time", DateUtils.dateToString(time, DateUtils.DATE_FORMAT_SS));
            json.put("timeLong", time.getTime());
            JSY_array.add(json);
        }

        return JSY_array;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String setShow(HttpServletRequest request) {

        String s_flag = "true";

        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String s_showList = RequestUtils.getString(request, "showList");
            String s_hideList = RequestUtils.getString(request, "hideList");
            JSONArray JSY_show = new JSONArray();
            JSONArray JSY_hide = new JSONArray();

            if (s_showList != null) {
                JSY_show = JSONArray.fromObject(s_showList);
            }

            if (s_hideList != null) {
                JSY_hide = JSONArray.fromObject(s_hideList);
            }

            for (int i = 0; i < JSY_show.size(); i++) {
                //条件
                DBObject DBO_condition = new BasicDBObject();
                DBO_condition.put("uuid", s_uuid);
                DBO_condition.put("pid", JSY_show.get(i));

                //修改数据
                DBObject DBO_update = new BasicDBObject();
                DBO_update.put("show", "1");

                db.update(TableName.T_WORK, DBO_condition, DBO_update);
            }

            for (int i = 0; i < JSY_hide.size(); i++) {
                //条件
                DBObject DBO_condition = new BasicDBObject();
                DBO_condition.put("uuid", s_uuid);
                DBO_condition.put("pid", JSY_hide.get(i));

                //修改数据
                DBObject updateDB = new BasicDBObject();
                updateDB.put("show", "0");

                db.update(TableName.T_WORK, DBO_condition, updateDB);
            }
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String setTop(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String s_functionid = RequestUtils.getString(request, "functionid");

            //条件
            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("pid", s_functionid);

            //修改数据
            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("insteadTop", "1");
            DBO_update.put("time", new Date());

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String setNotTop(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String s_functionid = RequestUtils.getString(request, "functionid");

            //条件
            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("pid", s_functionid);

            //修改数据
            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("insteadTop", "0");

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String addMsgCount(HttpServletRequest request) {

        String s_flag = "true";

        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String remark = RequestUtils.getString(request, "remark");//remark

            if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
                s_uuid = (String) request.getSession().getAttribute("uuid");
            }
            String s_functionid = RequestUtils.getString(request, "functionid");

            //条件
            DBObject DBO_condition = new BasicDBObject();

            if (s_uuid.startsWith("[")) {
                List<DBObject> conditions = new ArrayList<DBObject>();
                JSONArray JSY_uuids = JSONArray.fromObject(s_uuid);

                DBObject condition;
                for (int i = 0; i < JSY_uuids.size(); i++) {

                    condition = new BasicDBObject();
                    condition.put("uuid", JSY_uuids.get(i));
                    condition.put("pid", s_functionid);
                    conditions.add(condition);
                }
                DBO_condition.put("$or", conditions);

            } else {

                DBO_condition.put("uuid", s_uuid);
                DBO_condition.put("pid", s_functionid);
            }

            //修改数据
            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("count", 1);

            db.updateInt(TableName.T_WORK, DBO_condition, DBO_update);

            DBO_update = new BasicDBObject();
            DBO_update.put("time", new Date());
            DBO_update.put("remark", remark);

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String reduceMsgCount(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");

            if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
                s_uuid = (String) request.getSession().getAttribute("uuid");
            }
            String s_functionid = RequestUtils.getString(request, "functionid");

            //条件
            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("pid", s_functionid);

            //修改数据
            DBObject DBO_update = new BasicDBObject();
            DBObject workDBObject = db.findOne(TableName.T_WORK, DBO_condition);
            Integer count = (Integer) workDBObject.get("count");
            if (count <= 0) {
                count = 0;
            } else {
                count--;
            }
            DBO_update.put("count", count);
            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String deleteMsgCount(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");

            if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
                s_uuid = (String) request.getSession().getAttribute("uuid");
            }

            String s_functionid = RequestUtils.getString(request, "functionid");

            //条件
            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("pid", s_functionid);

            //修改数据
            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("count", 0);

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String updateGroupId(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String s_pid = RequestUtils.getString(request, "pid");//功能编号
            String s_groupId = RequestUtils.getString(request, "groupId");

            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("pid", s_pid);

            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("groupId", s_groupId);

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }

    /*-------------------------------------------------------------------------------------------*/

    public String deleteGroup(HttpServletRequest request) {

        String s_flag = "true";
        try {
            String s_uuid = RequestUtils.getString(request, "uuid");
            String s_groupId = RequestUtils.getString(request, "groupId");

            DBObject DBO_condition = new BasicDBObject();
            DBO_condition.put("uuid", s_uuid);
            DBO_condition.put("groupId", s_groupId);

            DBObject DBO_update = new BasicDBObject();
            DBO_update.put("groupId", "");

            db.update(TableName.T_WORK, DBO_condition, DBO_update);
        } catch (Exception e) {
            s_flag = "false";
        }

        return s_flag;
    }
}
